import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.Arrays;
public class week8 {
    static Random random = new Random();

    public static void test() {
        System.out.println(task1("abbc", "dog cat cat fish"));
        System.out.println(task2(new int[]{1, 2, 2, 3, 0}));
        for (int i = 0; i < 5; i++) {
            int target = random.nextInt(15) - 3;
            System.out.println("target:" + target + "\tresult:" + task3(new int[]
                    {0, 4, 5, 6, 8}, target));
        }
    }

    public static void main(String[] args) {
        test();
    }

    public static boolean task1(String pattern, String str) {
//第一题代码
        HashMap<Character,String> map = new HashMap<>();
        String[] words = str.split(" ");
        int len = words.length;
        if (len != pattern.length())
            return false;
        for (int i=0; i<len;i++){
            char c = pattern.charAt(i);
            if (!map.containsKey(c)){
                if (!map.containsValue(words[i])){
                    map.put(c,words[i]);
                }
                else{
                    return false;
                    }
            }
            else if (!map.get(c).equals(words[i])){
                return false;
            }
        }

        return true;
    }

    public static int task2(int[] nums) {
//第二题代码
        int res = 0;
        Set<Integer> set = new HashSet<>();
        for (int i = 0;i<nums.length;i++){
            if (!set.contains(nums[i])){
                set.add(nums[i]);
            }else{
                res = nums[i];
                break;
            }
        }
        return res;
    }

    public static int task3(int[] nums, int target) {
//第三题代码
        int i = Arrays.binarySearch(nums, target);
        return i;
    }
}